<template>
  <el-container style="height: 100%">
    <!-- 头部区域 -->
    <el-header>
      <div class="home">
        <img src="../assets/h.jpg" alt="logo" />
        <span style="cursor: pointer" @click="toHome">电商后台管理系统</span>
      </div>
      <el-button type="info" @click="loginOut">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="flag ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">{{ "|||" }}</div>
        <el-menu
          background-color="#172c36"
          text-color="#fff"
          active-text-color="#ffd04b"
          :unique-opened="true"
          :collapse="flag"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            v-for="menuItem in menuList"
            :key="menuItem.id"
            :index="'/h' + menuItem.path"
          >
            <template slot="title">
              <i :class="iconsObj[menuItem.id]" style="margin-right: 10px"></i>
              <span>{{ menuItem.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              v-for="menu in menuItem.children"
              :key="menu.id"
              :index="'/h' + menuItem.path+'/' + menu.path"
              ><template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ menu.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      menuList: [],
      iconsObj: {
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      flag: false,
      activePath: "",
    };
  },
  created() {
    this.getMenu();
    this.activePath = window.sessionStorage.getItem("path");
  },
  methods: {
    //退出登录
    loginOut() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    //获取左侧菜单
    async getMenu() {
      const res = await this.$http.reqMenu();
      if (res.meta.status === 200) {
        this.menuList = res.data;
      }
    },
    //点击按钮，切换菜单折叠与展开
    toggleCollapse() {
      this.flag = !this.flag;
    },
    toHome() {
      this.$router.push("/home");
    },
    //保存状态
    saveNavStatus() {
      const path = this.$route.path;
      window.sessionStorage.setItem("path", path);
      this.activePath = window.sessionStorage.getItem("path");
    },
  },
  watch: {
    $route: {
      handler(newRoute,oldRoute) {
         this.saveNavStatus()
      },
      immediate: true
    },
  },
};
</script>

<style lang="less" scoped>
div,
.el-submenu,
.el-menu-item {
  user-select: none;
}
.el-header {
  background-color: #1e2c31;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 20px;
    }
  }
  img {
    width: 50px;
    height: 50px;
  }
}
.el-aside {
  background-color: #172c36;
  .el-menu {
    border-right: none;
  }
  .toggle-button {
    text-align: center;
    background-color: #273136;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
}
</style>
